/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainFrame.java
 *
 * Created on 05/12/2011, 05:45:41 PM
 */
package appbastanteo1.view;

import appbastanteo1.BusinessException;
import appbastanteo1.controller.AdmGrupos;
import appbastanteo1.controller.AdmRepresentantes;
import appbastanteo1.controller.AdmBastanteos;
import appbastanteo1.controller.AdmPoderes;
import appbastanteo1.controller.AdmClientes;
import appbastanteo1.model.Grupo;
import appbastanteo1.model.Poder;
import appbastanteo1.model.Representante;
import java.text.ParseException;
import javax.swing.JInternalFrame;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author JuanJose.Fuchs
 */
public class MainForm extends javax.swing.JFrame {

    private DateFormat formatter;
    private AdmPoderes admPoderes;
    private AdmGrupos admGrupos;
    private AdmRepresentantes admRepresentantes;
    private AdmClientes admClientes;
    private AdmBastanteos admBastanteos;

    /** Creates new form MainFrame */
    public MainForm() {
        initComponents();

        formatter = new SimpleDateFormat("dd/MM/yyyy");

        admGrupos = new AdmGrupos();
        admPoderes = new AdmPoderes();
        admRepresentantes = new AdmRepresentantes();
        admClientes = new AdmClientes();
        admBastanteos = new AdmBastanteos();

        try {
            cargarDatosIniciales();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
            System.exit(0);
        }
    }

    private void cargarDatosIniciales() throws BusinessException, ParseException {
        cargarRepresentantes();
        cargarClientes();
        cargarGrupos();
        cargarPoderes();
        // cargarBastanteos();
    }

    private void cargarRepresentantes() throws BusinessException {
        admRepresentantes.registrarRepresentante(
                "DNI", "12345678", "Diego Alexis", "Seminario Capulian", "Legal", "A");
        admRepresentantes.registrarRepresentante(
                "DNI", "23456789", "Juan Jose", "Fuchs", "Legal", "B");
        admRepresentantes.registrarRepresentante(
                "DNI", "88884444", "Juan", "Perez Perez", "Legal", "B");
    }

    private void cargarClientes() throws BusinessException, ParseException {
        admClientes.registrarCliente(
                "CL000088", "12245726667", "Extreme IT Solutions",
                formatter.parse("01/01/2010"), "SAC", 10,
                admRepresentantes.obtenerRepresentante("12345678"));
        admClientes.registrarCliente(
                "CL000089", "2222333378", "La positiva Seguros",
                formatter.parse("02/01/2010"), "SAC", 100,
                admRepresentantes.obtenerRepresentante("23456789"));
        admClientes.registrarCliente(
                "CL000090", "2222333379", "Rimac Seguros",
                formatter.parse("02/01/2010"), "SAC", 110,
                admRepresentantes.obtenerRepresentante("88884444"));
    }

    private void cargarGrupos() throws BusinessException {
        admGrupos.registrarGrupo(1, "Poderes judiciales");
        admGrupos.registrarGrupo(2, "Poderes Penales");
        admGrupos.registrarGrupo(3, "Poderes varios");
    }

    private void cargarPoderes() throws BusinessException {
        admPoderes.registrarPoder(
                "CHCO", "Cobro de cheques", "Pasivo", admGrupos.obtenerGrupo(1));
        admPoderes.registrarPoder(
                "EFRE", "Retiro de efetivo", "Activo", admGrupos.obtenerGrupo(1));
        admPoderes.registrarPoder(
                "CETCO", "Cobro de Devengados", "Activo", admGrupos.obtenerGrupo(2));
        admPoderes.registrarPoder(
                "OTRO", "Otro poder", "Activo", admGrupos.obtenerGrupo(3));
    }

    private void cargarBastanteos() throws BusinessException, ParseException {
        ArrayList<Poder> poderes = new ArrayList<Poder>();

        poderes.add(admPoderes.buscarPoderes("CHCO"));
        poderes.add(admPoderes.buscarPoderes("OTRO"));
        admBastanteos.registrarBastanteo(
                admClientes.buscarCliente("CL000088"),
                admRepresentantes.obtenerRepresentante("12345678"),
                "A sola firma", (ArrayList<Poder>) poderes.clone(),
                formatter.parse("01/02/2012"), 1000000);
        admBastanteos.registrarBastanteo(
                admClientes.buscarCliente("CL000090"),
                admRepresentantes.obtenerRepresentante("88884444"),
                "En conjunto con", (ArrayList<Poder>) poderes.clone(),
                formatter.parse("24/12/2011"), 20000);

        poderes.clear();
        poderes.add(admPoderes.buscarPoderes("OTRO"));
        poderes.add(admPoderes.buscarPoderes("EFRE"));
        admBastanteos.registrarBastanteo(
                admClientes.buscarCliente("CL000089"),
                admRepresentantes.obtenerRepresentante("23456789"),
                "A sola firma", (ArrayList<Poder>) poderes.clone(),
                formatter.parse("01/12/2011"), 99999.98);

        poderes.clear();
        poderes.add(admPoderes.buscarPoderes("EFRE"));
        poderes.add(admPoderes.buscarPoderes("CETCO"));
        admBastanteos.registrarBastanteo(
                admClientes.buscarCliente("CL000090"),
                admRepresentantes.obtenerRepresentante("88884444"),
                "A sola firma", (ArrayList<Poder>) poderes.clone(),
                formatter.parse("01/03/2013"), 1500);
    }

    private void abrirFormulario(int idFormulario) {
        JInternalFrame frame = null;
        switch (idFormulario) {
            case 1:
                frame = new VistaGrupos(admGrupos);
                break;
            case 2:
                frame = new VistaPoderes(admPoderes, admGrupos);
                break;
            case 3:
                frame = new VistaRepresentantes(admRepresentantes);
                break;
            case 4:
                frame = new VistaClientes(admClientes, admRepresentantes);
                break;
            case 5:
                frame = new VistaBastanteos(
                        admBastanteos, admGrupos, admPoderes, admClientes);
                break;
            case 6:
                frame = new VistaConsultarPoderes(admBastanteos);
                break;
            case 7:
                frame = new VistaVerificarPoderes(admBastanteos);
                break;
        }
        this.desktopPane.removeAll();
        this.desktopPane.repaint();
        this.desktopPane.add(frame);
        frame.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        desktopPane = new javax.swing.JDesktopPane();
        menuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        exitMenuItem = new javax.swing.JMenuItem();
        mnuAdministrar = new javax.swing.JMenu();
        mnuGrupos = new javax.swing.JMenuItem();
        mnuPoderes = new javax.swing.JMenuItem();
        mnuRepresentantes = new javax.swing.JMenuItem();
        mnuClientes = new javax.swing.JMenuItem();
        mnuBastantear = new javax.swing.JMenu();
        mnuBastantearPoderes = new javax.swing.JMenuItem();
        mnuConsultas = new javax.swing.JMenu();
        mnuConsultarPoderes = new javax.swing.JMenuItem();
        mnuVerificarPoderes = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Administración de Bastanteos");

        fileMenu.setText("File");

        exitMenuItem.setText("Exit");
        exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        mnuAdministrar.setText("Administrar");

        mnuGrupos.setText("Grupos");
        mnuGrupos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuGruposActionPerformed(evt);
            }
        });
        mnuAdministrar.add(mnuGrupos);

        mnuPoderes.setText("Poderes");
        mnuPoderes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuPoderesActionPerformed(evt);
            }
        });
        mnuAdministrar.add(mnuPoderes);

        mnuRepresentantes.setText("Representantes");
        mnuRepresentantes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuRepresentantesActionPerformed(evt);
            }
        });
        mnuAdministrar.add(mnuRepresentantes);

        mnuClientes.setText("Clientes");
        mnuClientes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuClientesActionPerformed(evt);
            }
        });
        mnuAdministrar.add(mnuClientes);

        menuBar.add(mnuAdministrar);

        mnuBastantear.setText("Bastanteo");
        mnuBastantear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuBastantearActionPerformed(evt);
            }
        });

        mnuBastantearPoderes.setText("Bastantear Poderes");
        mnuBastantearPoderes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuBastantearPoderesActionPerformed(evt);
            }
        });
        mnuBastantear.add(mnuBastantearPoderes);

        menuBar.add(mnuBastantear);

        mnuConsultas.setText("Consultas");

        mnuConsultarPoderes.setText("Consultar poderes");
        mnuConsultarPoderes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuConsultarPoderesActionPerformed(evt);
            }
        });
        mnuConsultas.add(mnuConsultarPoderes);

        mnuVerificarPoderes.setText("Verificar poderes");
        mnuVerificarPoderes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuVerificarPoderesActionPerformed(evt);
            }
        });
        mnuConsultas.add(mnuVerificarPoderes);

        menuBar.add(mnuConsultas);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(desktopPane, javax.swing.GroupLayout.DEFAULT_SIZE, 990, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(desktopPane, javax.swing.GroupLayout.DEFAULT_SIZE, 523, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
        System.exit(0);
    }//GEN-LAST:event_exitMenuItemActionPerformed

    private void mnuPoderesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuPoderesActionPerformed
        abrirFormulario(2);
    }//GEN-LAST:event_mnuPoderesActionPerformed

    private void mnuGruposActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuGruposActionPerformed
        abrirFormulario(1);
    }//GEN-LAST:event_mnuGruposActionPerformed

    private void mnuRepresentantesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuRepresentantesActionPerformed
        abrirFormulario(3);
    }//GEN-LAST:event_mnuRepresentantesActionPerformed

    private void mnuClientesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuClientesActionPerformed
        abrirFormulario(4);
    }//GEN-LAST:event_mnuClientesActionPerformed

    private void mnuBastantearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuBastantearActionPerformed
    }//GEN-LAST:event_mnuBastantearActionPerformed

    private void mnuBastantearPoderesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuBastantearPoderesActionPerformed
        abrirFormulario(5);
    }//GEN-LAST:event_mnuBastantearPoderesActionPerformed

    private void mnuConsultarPoderesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuConsultarPoderesActionPerformed
        abrirFormulario(6);
    }//GEN-LAST:event_mnuConsultarPoderesActionPerformed

    private void mnuVerificarPoderesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuVerificarPoderesActionPerformed
        abrirFormulario(7);
    }//GEN-LAST:event_mnuVerificarPoderesActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MainForm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JDesktopPane desktopPane;
    private javax.swing.JMenuItem exitMenuItem;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenu mnuAdministrar;
    private javax.swing.JMenu mnuBastantear;
    private javax.swing.JMenuItem mnuBastantearPoderes;
    private javax.swing.JMenuItem mnuClientes;
    private javax.swing.JMenuItem mnuConsultarPoderes;
    private javax.swing.JMenu mnuConsultas;
    private javax.swing.JMenuItem mnuGrupos;
    private javax.swing.JMenuItem mnuPoderes;
    private javax.swing.JMenuItem mnuRepresentantes;
    private javax.swing.JMenuItem mnuVerificarPoderes;
    // End of variables declaration//GEN-END:variables
}
